package datastructure.matrix;

/**
 * each row and each column is sorted 
 * find an element from it
 * assume in row, it is ascending order,
 *        in column (top down), it is ascending order 
 * 
 * @author Lei
 */
public class FindElementInSortedMaxtrix {

    public boolean findElementInSortedMatrix(int element, int[][] matrix) {
        if (matrix == null) {
            return false;
        }

        int row = 0;
        int column = matrix[0].length - 1;
        while (row < matrix.length && column >= 0) {
            if (matrix[row][column] == element) {
                return true;
            }
            else if (matrix[row][column] > element) {
                row++;
            }
            else {
                column--;
            }
        }
        return false;

    }
}
